/**
 *
 * @Author: Marek Zakowicz <mazak (at) debian (dot) linux.org.pl>
 *
 * Copyright (c) 2005, 2006, 2007 Marek Zakowicz. All rights reserved.
 *
 * Licence: This code may be used, (re)distribute and/or modify under the terms one of the following two kinds of licence:
 *          1) as free software under GPL 3 (General Public Licence ver. 3 as published by the Free Software Foundation)
 *          2) as software under the terms individual licence from Marek Zakowicz
 *
 *          This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *          without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

package jsp2php;

class StaticIntegerTranslator extends TranslatorBase {

   StaticIntegerTranslator() {
      fields.put("MAX_VALUE", IntValueTranslator.literal(Integer.MAX_VALUE) );
      fields.put("MIN_VALUE", new Translation("(int) -2147483648", JavaType.integer(), false, true) );
      fields.put("SIZE", IntValueTranslator.literal(32) );
   }

   public String name() {
      return "java.lang.Integer";
   }

   // TRANSLATOR

   public Translation translate_parseInt(Translation ... args) throws TranslatorException {
      validate_arg_no(new int[]{1, 2}, "parseInt", args);
      validate_arg_type(new JavaType[]{JavaType.string(), JavaType.integer()}, "parseInt", args);

      Translation arg = args[1];

      switch(args.length) {
         case 2: {
            return cast(JavaType.integer(), args[1]);
         }
         case 3: {
            return new Translation("(int) base_convert("+ arg.text +", "+ args[2].text +", 10)", JavaType.integer(), false, arg.not_null);
         }
      }
      throw new TranslatorException("Integer class supports method parseInt(String [, int]), but needed "+ TranslatorUtil.call_description(args) +"!");
   }

   public Translation translate_toBinaryString(Translation ... args) throws TranslatorException {
      return static_translate("toBinaryString", "decbin(%s)", JavaType.integer(), JavaType.string(), args);
   }

   public Translation translate_toHexString(Translation ... args) throws TranslatorException {
      return static_translate("toHexString", "dechex(%s)", JavaType.integer(), JavaType.string(), args);
   }

   public Translation translate_toOctalString(Translation ... args) throws TranslatorException {
      return static_translate("toOctalString", "decoct(%s)", JavaType.integer(), JavaType.string(), args);
   }

   public Translation translate_toString(Translation ... args) throws TranslatorException {
      validate_arg_no(new int[]{1, 2}, "toString", args);
      validate_arg_type(new JavaType[]{JavaType.integer(), JavaType.integer()}, "toString", args);

      Translation arg = args[1];

      switch(args.length) {
         case 2: {
            return cast(JavaType.string(), arg);
         }
         case 3: {
            return new Translation("base_convert("+ cast(JavaType.string(), arg).text +", 10, "+ args[2].text +")", JavaType.string(), true, arg.not_null);
         }
      }
      throw new TranslatorException("Integer class supports method toString(int [, int]), but needed "+ TranslatorUtil.call_description(args) +"!");
   }
}


/**
 * @composed 1 - 1 IntegerValueTranslator
 */
class IntegerTranslator extends StaticIntegerTranslator implements ITypeTranslator {
   private static IntegerValueTranslator value_translator = new IntegerValueTranslator();

   static {
      ITypeTranslator tr = new IntegerTranslator();
      //JavaType.add_type_translator("java.lang.Integer", tr);
      //JavaType.add_type_translator("Integer", tr);
      JavaType.add_type_translator( tr );
   }

   private IntegerTranslator() {}

   public IValueTranslator value_translator() {
      return value_translator;
   }
}

class IntegerValueTranslator extends StaticIntegerTranslator implements IValueTranslator {
   // TODO: (Un-)boxing przez delegacje do Int
}
